package com.xw.xwpermission;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;

import com.xw.selector.MediaSelector;
import com.xw.selector.MimeType;
import com.xw.selector.entity.CaptureStrategy;
import com.xw.selector.ui.CustomVideoActivity;
import com.xw.xpermission.XPermissions;

import java.io.IOException;
import java.util.List;

import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;

public class MainActivity extends AppCompatActivity {

    private Camera camera;
    private SurfaceView surfaceView;
    private Disposable disposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        XPermissions rxPermissions = new XPermissions(this);

        surfaceView = findViewById(R.id.surfaceView);
        findViewById(R.id.enableCamera).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                rxPermissions.ensureEach(Manifest.permission.CAMERA)

                rxPermissions
                        .setReasonMessage("申请相机权限，用于显示Camera相机画面采集功能")
                        .request(Manifest.permission.CAMERA)
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean aBoolean) throws Throwable {
                                if (aBoolean) {
                                    releaseCamera();
                                    camera = Camera.open(0);
                                    try {
                                        camera.setPreviewDisplay(surfaceView.getHolder());
                                        camera.startPreview();
                                    } catch (IOException e) {
                                        Log.e("info", "Error while trying to display the camera preview", e);
                                    }
                                } else {
                                    Toast.makeText(MainActivity.this, "请开启必要的权限", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
            }
        });

        findViewById(R.id.btn_selector).setOnClickListener(v -> {
            rxPermissions.setReasonMessage("申请存储、相机权限，用于使用访问相册图片或调用系统相机拍照功能")
                    .request(
                            Manifest.permission.CAMERA,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE
                    )
                    .subscribe(aBoolean -> {

                        if (aBoolean) {
                            MediaSelector
                                    .create(this)
                                    .choose(MimeType.ofImage())
                                    .showCamera(true)
                                    .captureStrategy(new CaptureStrategy(true, "com.xw.xwpermission.MyProvider"))
                                    .maxSelectable(9)
                                    .imageEngine(new GlideEngine())
                                    .start(10011);
                        } else {
                            Toast.makeText(this, "请开启必要得权限", Toast.LENGTH_SHORT).show();
                        }
                    });

        });
    }

    @Override
    protected void onDestroy() {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
        super.onDestroy();
    }

    @Override
    protected void onStop() {
        super.onStop();
        releaseCamera();
    }

    private void releaseCamera() {
        if (camera != null) {
            camera.release();
            camera = null;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) return;

        if (resultCode == RESULT_OK) {
            //这个地方只返回视频地址
            if (requestCode == 10011) {
                List<String> pathList = MediaSelector.obtainPathsResult(data);
                for (String s : pathList) {
                    Log.d("info", "===图片地址===" + s);
                }
            }
        }
    }
}